<?php
$tableUser = 'v1user';

try {
  $hostname = HOST_NAME;
  $username = USER_NAME1;
  $password = PASSWORD1;
  $dbname = DATABASE_NAME1;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  $username1 = USER_NAME2;
  $password1 = PASSWORD2;
  $dbname1 = DATABASE_NAME2;
  $myPdo1 = new PDO ('mysql:host=' . $hostname, $username1, $password1);
  $myPdo1->exec('USE ' . $dbname1);
  $myPdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
} catch (PDOException $e) {
  echo $e->getMessage ();
}

/*
 * 读取数据并返回对象
 * 
 * */
function getall($sql,$fetchmethod='')
{
	global $myPdo;
	$data = array();
	
	$fetchmethod = empty($fetchmethod) ? PDO::FETCH_OBJ : PDO::FETCH_ASSOC ;
		
    $stmt2 = $myPdo->prepare($sql);
    $stmt2->execute();

    while($tmp2 = $stmt2->fetch($fetchmethod)){
    	/*print_r($tmp2) ;
    	echo "\n";
    	echo "\n";*/
    	
    	foreach(array('date','createdDate','deadline','sentDate','receivedDate','when','cancelledDate') as $chdate)
    	{
    		if(isset($tmp2->$chdate))
    		{
	            /*echo $chdate;
	            echo "\n";
	            echo "**************string to object************************";
	            print_r(SimpleDate::fromStamp($tmp2->$chdate));
	            echo "**************string to object************************";
	            echo "\n";*/    			
    			$tmp2->$chdate = ($tmp2->$chdate != 0) ? SimpleDate::fromStamp($tmp2->$chdate) : NULL;
    			/*echo "*******转换后的日期:****";
    			echo "\n";
    			print_r($tmp2->$chdate);
    			echo "\n";*/
    		}
    		
    	}
    	
      	$data[] = $tmp2;
      }

      return $data;
}

function count_num($sql){
	global $myPdo;
	
    $stmt2 = $myPdo->prepare($sql);
    $stmt2->execute();
	
    return $stmt2->rowCount();
}

/**
 * 执行sql
 * */
function sql_execute($sql){
    global $myPdo;	
    
    $stmt2 = $myPdo->prepare($sql);
    $stmt2->execute();

    return;
}


/*
 * 取得下一级的所有数据
 * $utable 表名,$parentId字段名,$e_oid字段的值
 * $condition='' 查询条件 
 * $dasc='' 排序字段
 * */
function getsonall($utable,$parentId,$e_oid,$condition='',$dasc=''){
	global $myPdo;	
	$data = array();
	
	$sql="select *,e_oid as id from ".$utable." where ".$parentId." = '".$e_oid."'";
	
	if(!empty($condition)) $sql .= " and $condition ";
	
	if(!empty($dasc)) $sql .= " order by ".$dasc." ";
	
	return getall($sql);
	
}

function lastinsertid(){
	global $myPdo;
	
	return $myPdo->lastInsertId();
}

/**
 *取得下一级的所有e_oid
 *$utable要查询的表,
 *$getfields 需要查询出来的字段,
 *$parentId 查询条件字段,
 *$e_oid 查询的值,
 *$condition=''其他条件,
 *$returnstr='' 返回类似于：1,2,3,4,5这种字符串
 */
 
 function getsonid($utable,$getfields,$parentId,$e_oid,$condition='',$returnstr='')
 {
 	global $myPdo;	
	$data = array();
	$new_data = array();
    
	$sql = " select ".$getfields."  from  ".$utable." where ".$parentId." = '".$e_oid."'  ";
	if(!empty($condition)) $sql .= " and  ".$condition." ";
	
	 $data = getall($sql,1);
	 
		if(empty($returnstr))
		{
		   if(!empty($data))
		   {
                foreach($data as $value)
                {
                    $new_data[] = $value['e_oid'];				
				}				
		      
		     
		     
		   }
		   return $new_data;
		
		
		    return  $data;
		}else{
		    $strid = '';
           	
			if(!empty($data))
			{
			   
			    foreach($data as $value)
				{
				    $strid .= $value['e_oid'].','; 
				}
			    
				$strid = substr($strid,0,-1);
			}
            
            return $strid;			
		
		}
 }



          /**
           * 
           * v2_purchaseItems operate
           * 
           */
          function v2_purchaseItems_operate($v2purchaseitem,$debug='')
          {
                foreach ($v2purchaseitem as $v2pitem)
                {
                    if(isset($v2pitem->id) || isset($v2pitem->e_oid))
                    {//update
                        $v2pitemsql ='update ns_producepurchaseitem set ';
                        foreach ($v2pitem as $key => $value)
                        {
                            if(!in_array($key,array('v2_selects','e_oid','id')))
                            {
                               $v2pitemsql .= '`'.$key."`='".addslashes($value)."',";
                            }
                        }
                        
                        $v2pitemsql =substr($v2pitemsql,0,-1);
                        
                        $v2pitemsql .= " where e_oid = '".$v2pitem->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2pitemsql."\n";
                        }else{
                            sql_execute($v2pitemsql);
                        }
                        unset($key);
                        unset($value);
                        
                            /**
                             * v2_selects
                             */
                              if(!empty($v2pitem->v2_selects))
                              {
                                  foreach ($v2pitem->v2_selects as $v2ptiemselect)
                                  {
                                     if(isset($v2ptiemselect->id) || isset($v2ptiemselect->e_oid))
                                     {//update
                                        $v2pitemselectsql = "update ns_producepurchaseitemselect set ";
                                        foreach ($v2ptiemselect as $key => $value)
                                        {
                                            if(!in_array($key,array('e_oid','id')))
                                            {
                                               $v2pitemselectsql .= '`'.$key."`='".addslashes($value)."',";
                                            }
                                        }
                                        $v2pitemselectsql =substr($v2pitemselectsql,0,-1);
                                        
                                        $v2pitemselectsql .= " where e_oid = '".$v2ptiemselect->id."'";  

                                        if(!empty($debug))
                                        {
                                            echo $v2pitemselectsql."\n";
                                        }else{
                                            sql_execute($v2pitemselectsql);
                                        }
                                        
                                     }else{//addd
                                        
                                        $v2pitemselectfield = '';
                                        $v2pitemselectvalue = '';
                                        
                                        foreach ($v2ptiemselect as $key => $value)
                                        {
                                            $v2pitemselectfield .='`'.$key.'`,';
                                            $v2pitemselectvalue .= " '".addslashes($value)."',"; 
                                            
                                        }
                                        
                                        $v2pitemselectfield = substr($v2pitemselectfield,0,-1);
                                        $v2pitemselectvalue = substr($v2pitemselectvalue,0,-1);  

                                        $v2pitemselectsql = "insert into ns_producepurchaseitemselect (`e_oid`,".$v2pitemselectfield.") values(NULL,".$v2pitemselectvalue.")";
                                        
                                        if(!empty($debug))
                                        {
                                            echo $v2pitemselectsql."\n";
                                        }else{
                                            sql_execute($v2pitemselectsql);
                                        }
                                     }//end add $v2ptiemselect
                                    
                                     unset($key);
                                     unset($value);
                                  }
                                
                                
                              }//not null
                              
                    }else{//purchaseitemadd
                        
                        $v2pitemfield = '';
                        $v2pitemvalue = '';
                        
                        foreach ($v2pitem as $key => $value)
                        {
                        	if(!in_array($key,array('v2_selects','selects','notes','_added','cancelled','isFree','urgent','order_id')))
                        	{
                        		$v2pitemfield .='`'.$key.'`,';
                                $v2pitemvalue .= " '".addslashes($value)."',"; 
                        	}
                        }
                        
                        $v2pitemfield = substr($v2pitemfield,0,-1);
                        $v2pitemvalue = substr($v2pitemvalue,0,-1);

                        $v2pitemsql = "insert into ns_producepurchaseitem(`e_oid`,".$v2pitemfield.") values(NULL,".$v2pitemvalue.")";  

                        if(!empty($debug))
                        {
                            echo $v2pitemsql."\n";
                        }else{
                            sql_execute($v2pitemsql);
                        }
                           unset($key);
                           unset($value);
                            /**
                             * $v2ptiemselect add
                             */
                             if(!empty($v2pitem->v2_selects))
                             {
                             	foreach ($v2pitem->v2_selects as $v2pitemselect)
                             	{
	                                $v2pitemselectfield = '';
	                                $v2pitemselectvalue = '';
	                                
	                                foreach ($v2pitemselect as $key => $value)
	                                {
	                                	if(!in_array($key,array('purchaseitem_id')))
	                                	{
	                                		$v2pitemselectfield .='`'.$key.'`,';
	                                		$v2pitemselectvalue .= " '".addslashes($value)."',";
	                                	}
	                                    
	                                }
	                                
	                                $v2pitemselectfield = substr($v2pitemselectfield,0,-1);
	                                $v2pitemselectvalue = substr($v2pitemselectvalue,0,-1);
	
	                                $v2pitemselectsql = "insert into ns_producepurchaseitemselect(`e_oid`,".$v2pitemselectfield.") values(NULL,".$v2pitemselectvalue.")";    
	
	                                if(!empty($debug))
	                                {
	                                    echo $v2pitemselectsql."\n";
	                                }else{
	                                    sql_execute($v2pitemselectsql);
	                                }
	                                unset($key);
	                                unset($value);
                                
                             	}//one v2purchaseitemselect
                             	
                             }//end v2purchaseitemselect add
                        
                    }//end purchaseitemadd
                    
                }           
            
          }



function sendEmailOperate($to,$title,$content)
{
  require_once dirname(__FILE__) . '/../library/phpMailer/class.phpmailer.php';

  $mailer = new PHPMailer();
  $mailer->IsSMTP();
  $mailer->Host = MAIL_HOST;
  if (MAIL_SMTP)
   {
     $mailer->SMTPAuth = MAIL_SMTP;
   }
      
   $mailer->Username = MAIL_USER; 
   $mailer->Password = MAIL_PASSWORD;
   $mailer->From = MAIL_FROM;
   $mailer->FromName = MAIL_NAME;
   $mailer->Body = $content;
   $mailer->Subject = $title;

   if ($to != '') {
        $mailer->AddAddress($to);
      }
      
   if (!$mailer->Send()) {
        echo $mailer->ErrorInfo;
      }
}































?>